from pages.trade_page import TradePage
from tools.driver import open_dingstock
from unittest import TestCase
from pages.recommend_page import RecommendPage
from pages.mine_page import MinePage
from pages.newest_page import NewestPage
import unittest
from time import sleep
from tools.driver import open_device_atx_server


class TestPublishTrade(TestCase):
    def setUp(self) -> None:
        self.driver = open_dingstock()
        self.recommendpage = RecommendPage(self.driver)
        self.minepage = MinePage(self.driver)
        self.newestpage = NewestPage(self.driver)
        self.tradepage = TradePage(self.driver)

    def tearDown(self) -> None:
        sleep(1)

    # @unittest.skip('安卓忽略发布交易，闪退')
    def test_a_publish_trade(self):
        """测试发布交易"""
        trade_text = '测试一下发布交易'
        trade_price = "999"
        self.recommendpage.click_add_button()
        self.recommendpage.choose_add_trade()
        self.recommendpage.input_trade_text(trade_text)
        self.recommendpage.choose_trade_image()
        self.recommendpage.choose_trade_goods(trade_price)
        trade_goods = self.recommendpage.get_trade_goods_name()
        self.recommendpage.click_publish()
        sleep(2)
        self.minepage.switch_home()
        self.recommendpage.switch_trade()
        sleep(1)
        self.tradepage.into_newest_trade_detail()
        text = self.tradepage.get_trade_content()
        price = self.tradepage.get_trade_price()
        name = self.tradepage.get_first_trade_name()
        assert name == trade_goods and text == trade_text and price == trade_price

    def test_b_like_trade(self):
        """测试交易点赞和取消点赞"""
        self.recommendpage.switch_trade()
        origin_like = int(self.tradepage.get_first_trade_like_num())
        self.tradepage.like_trade()
        after_like = int(self.tradepage.get_first_trade_like_num())
        self.tradepage.like_trade()
        cancel_like = int(self.tradepage.get_first_trade_like_num())
        assert origin_like+1 == after_like and after_like-1 == cancel_like

    def test_c_collect_trade(self):
        """测试收藏交易"""
        self.recommendpage.switch_trade()
        self.tradepage.into_newest_trade_detail()
        trade_content = self.tradepage.get_trade_content()
        self.tradepage.collect_trade()
        self.tradepage.trade_detail_back()
        self.recommendpage.switch_mine()
        self.minepage.click_menu()
        self.minepage.click_my_collect()
        self.minepage.click_my_collect_sale()
        my_content = self.minepage.get_my_collect_sale_content()
        assert trade_content == my_content

    def test_d_cancel_trade_collect(self):
        """测试取消收藏交易"""
        self.recommendpage.switch_trade()
        sleep(1)
        self.tradepage.into_newest_trade_detail()
        self.tradepage.collect_trade()
        assert self.tradepage.is_text_toast_exist('取消收藏成功')

    def test_e_delete_trade(self):
        """测试删除交易"""
        self.recommendpage.switch_trade()
        self.tradepage.into_newest_trade_detail()
        origin = self.tradepage.get_trade_content()
        self.tradepage.trade_detail_back()
        self.tradepage.click_newest_trade_menu()
        self.tradepage.click_delete()
        sleep(1)
        self.tradepage.confirm_delete()
        self.tradepage.into_newest_trade_detail()
        new = self.tradepage.get_trade_content()
        assert origin != new

    @unittest.skip('由于举报成功弹toast无法捕捉，跳过用例')
    def test_f_report_trade(self):
        """测试举报交易"""
        self.recommendpage.switch_trade()
        self.tradepage.report_third_trade()
        assert self.tradepage.is_text_toast_exist('举报成功，我们将尽快处理')


if __name__ == '__main__':
    unittest.main()